package com.linjuke.mayouting.biz.dal.ibatis;

import java.util.List;
import java.util.Map;

import com.linjuke.mayouting.biz.ao.query.RoleQuery;
import com.linjuke.mayouting.biz.dal.daointerface.RoleDAO;
import com.linjuke.mayouting.biz.dal.dataobject.RoleDO;
import com.zjuh.sweet.lang.CollectionUtil;

public class RoleDAOIbatis extends BaseIbatisDAO implements RoleDAO {

	@Override
	public int create(RoleDO roleDO) {
		return (Integer)this.getSqlMapClientTemplate().insert("RoleDAO.create", roleDO);
	}

	@Override
	public RoleDO queryById(int id) {
		return (RoleDO)this.getSqlMapClientTemplate().queryForObject("RoleDAO.queryById", id);
	}

	@Override
	public int update(RoleDO roleDO) {
		return this.getSqlMapClientTemplate().update("RoleDAO.update", roleDO);
	}

	@Override
	public int delete(int id) {
		return this.getSqlMapClientTemplate().update("RoleDAO.delete", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleDO> queryRoleByUserId(int userId) {
		return (List<RoleDO>)this.getSqlMapClientTemplate().queryForList("RoleDAO.queryRoleByUserId", userId);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Integer> queryPermissionIdsByUserId(int userId) {
		return (List<Integer>)this.getSqlMapClientTemplate().queryForList("RoleDAO.queryPermissionIdsByUserId", userId);
	}
	
	@SuppressWarnings("unchecked")
	public List<Integer> queryPermissionIdsByRoleId(int roleId) {
		return (List<Integer>)this.getSqlMapClientTemplate().queryForList("RoleDAO.queryPermissionIdsByRoleId", roleId);
	}
	
	public void addUser(int roleId, int userId) {
		Map<String, Integer> params = CollectionUtil.newHashMap();
		params.put("roleId", roleId);
		params.put("userId", userId);
		getSqlMapClientTemplate().insert("RoleDAO.addUser", params);
	}
	
	public void removeUser(int roleId, int userId) {
		Map<String, Integer> params = CollectionUtil.newHashMap();
		params.put("roleId", roleId);
		params.put("userId", userId);
		getSqlMapClientTemplate().delete("RoleDAO.removeUser", params);
	}
	
	public void addPermission(int roleId, int permissionId) {
		Map<String, Integer> params = CollectionUtil.newHashMap();
		params.put("roleId", roleId);
		params.put("permissionId", permissionId);
		getSqlMapClientTemplate().insert("RoleDAO.addPermission", params);
	}
	
	public void removePermission(int roleId, int permissionId) {
		Map<String, Integer> params = CollectionUtil.newHashMap();
		params.put("roleId", roleId);
		params.put("permissionId", permissionId);
		getSqlMapClientTemplate().delete("RoleDAO.removePermission", params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleDO> queryForPage(RoleQuery roleQuery) {
		int count = (Integer)this.getSqlMapClientTemplate().queryForObject("RoleDAO.queryForPageCount", roleQuery);
		List<RoleDO> roles = (List<RoleDO>)this.getSqlMapClientTemplate().queryForList("RoleDAO.queryForPage", roleQuery);
		roleQuery.setTotalResultCount(count);
		return roles;
	}

	@Override
	public int removeAllForUser(int userId) {
		return this.getSqlMapClientTemplate().update("RoleDAO.removeAllForUser", userId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> queryUserIdByRoleId(int roleId) {
		return (List<Integer>)this.getSqlMapClientTemplate().queryForList("RoleDAO.queryUserIdByRoleId", roleId);
	}

	@Override
	public int removePermissions(int roleId) {
		return this.getSqlMapClientTemplate().update("RoleDAO.removePermissions", roleId);
	}

	@Override
	public int removeUsers(int roleId) {
		return this.getSqlMapClientTemplate().update("RoleDAO.removeUsers", roleId);	
	}
	
	
}
